<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2021-09-07
 * Time: 13:12
 */

namespace app\services;


use app\models\BroadcastPositions;
use app\models\Broadcasts;

class BroadcastService extends ServiceBase
{
    /**
     * @param array $select
     * @param int $page
     * @param int $pagesize
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLists($select = [], $page = 1, $pagesize = 10)
    {
        $broadcasts = Broadcasts::lists($select, $page, $pagesize);

        return $broadcasts;
    }

    /**
     * @param $newsId
     * @return Broadcasts|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDetail($broadcastId)
    {
        $broadcast = Broadcasts::find($broadcastId);

        if ($broadcast instanceof Broadcasts) {
            return $broadcast;
        }

        throw new \Exception('轮播图不存在');
    }

    /**
     * 获取所有的轮播图位置
     * @return BroadcastPositions|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPositions()
    {
        $positions = BroadcastPositions::select();

        return $positions;
    }

    /**
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function create($params)
    {
        try {
            $broadcast = new Broadcasts();
            $broadcast->positionId = $params['positionId'];
            $broadcast->appletId = $params['appletId'];
            $broadcast->broadcastName = $params['broadcastName'];
            $broadcast->broadcastImage = $params['broadcastImage'];
            $broadcast->broadcastUrl = $params['broadcastUrl'];
            $broadcast->sort = isset($params['sort'])?$params['sort']:0;
            $broadcast->save();

            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param Broadcasts $broadcast
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function update(Broadcasts $broadcast, $params)
    {
        try {
            foreach ($params as $key => $val) {
                $broadcast->$key = $val;
            }
            $broadcast->save();

            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param Broadcasts $broadcast
     * @return bool
     * @throws \Exception
     */
    public function enable(Broadcasts $broadcast)
    {
        try {
            $broadcast->status = 1;
            $broadcast->save();

            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param Broadcasts $broadcast
     * @return bool
     * @throws \Exception
     */
    public function disable(Broadcasts $broadcast)
    {
        try {
            $broadcast->status = 0;
            $broadcast->save();

            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param Broadcasts $broadcast
     * @return bool
     * @throws \Exception
     */
    public function delete(Broadcasts $broadcast)
    {
        try {
            $broadcast->delete();

            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param $positionId
     * @return Broadcasts[]|array|\think\Collection
     */
    public function getBroadcastByPositionId($positionId, $appletId = 1)
    {
        try {
            if (!$positionId) {
                throw new \Exception('请填写广告位置');
            }

            $broadcasts = Broadcasts::where(['positionId' => $positionId, 'appletId' => $appletId, 'status' => 1])->select();

            return $broadcasts;

        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }
}